package classes.rocks {
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.filters.BitmapFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.filters.BlurFilter;
	import flash.ui.Mouse;
	import flash.utils.getDefinitionByName;

	import com.mttang.event.JEvent;
	import com.mttang.event.CommonEvent;

	/**
	 * ...
	 * @author fox
	 */
	public class CreateMan extends MovieClip {
		private var currentStr:String;
		private var tmpStr:String;
		private var currentRockMan:*;

		public function CreateMan(){
			man_con.RockManA.buttonMode = true;
			man_con.RockManB.buttonMode = true;
			man_con.RockManC.buttonMode = true;
			man_con.RockManD.buttonMode = true;
			man_con.RockManE.buttonMode = true;
			man_con.RockManF.buttonMode = true;
			man_con.RockManG.buttonMode = true;
			man_con.RockManH.buttonMode = true;			
			addListener(man_con.RockManA);
			addListener(man_con.RockManB);
			addListener(man_con.RockManC);
			addListener(man_con.RockManD);
			addListener(man_con.RockManE);
			addListener(man_con.RockManF);
			addListener(man_con.RockManG);
			addListener(man_con.RockManH);
		}
		
		private function addListener(_dispatch:*):void {
			_dispatch.mouseChildren = false;
			_dispatch.addEventListener(MouseEvent.CLICK, createClickHandler);
			_dispatch.addEventListener(MouseEvent.MOUSE_OVER, createOverHandler);
			_dispatch.addEventListener(MouseEvent.MOUSE_OUT, createOutHandler);
		}
		
		private function removeListener(_dispatch:*):void {
			_dispatch.removeEventListener(MouseEvent.CLICK, createClickHandler);
			_dispatch.removeEventListener(MouseEvent.MOUSE_OVER, createOverHandler);
			_dispatch.removeEventListener(MouseEvent.MOUSE_OUT, createOutHandler);
		}

		private function createClickHandler(e:MouseEvent):void {			
			(e.target as MovieClip).alpha = 0;
			currentStr = (e.target.name).split("RockMan")[1];
			if ((currentStr >= "A") && (currentStr <= "H")){
				if (currentRockMan){
					removeChild(currentRockMan);
					currentRockMan = null;
				}
				var rockManCls:Class = getDefinitionByName("RockMan" + currentStr) as Class;
				currentRockMan = new rockManCls();
				currentRockMan.x = 80;
				currentRockMan.y = 200;
				addChildAt(currentRockMan, numChildren);
				btn_go.addEventListener(MouseEvent.CLICK, createGoHandler);
			}
		}

		private function createOverHandler(e:MouseEvent):void {
			(e.target as MovieClip).alpha = 0;
			tmpStr=(e.target.name).split("RockMan")[1];
			if ((tmpStr >= "A") && (tmpStr <= "H") && (tmpStr!=null)) {
				var filter:GlowFilter = getBitmapFilter();
				man_con["RockMan" + tmpStr].filters = [filter];
			}
		}

		private function createOutHandler(e:MouseEvent):void {
			(e.target as MovieClip).alpha = 0.3;
			//Mouse.show();
			if ((tmpStr >= "A") && (tmpStr <= "H")){
				man_con["RockMan" + tmpStr].filters = null;
			}
		}

		private function createGoHandler(e:MouseEvent):void {
			//trace("currentStr=",currentStr);
			if (currentStr != null) {
				var _obj:Object = new Object();
				_obj.manid = "RockMan" + currentStr;
				dispatchEvent(new JEvent(CommonEvent.CREATE_ROCKMAN_EVENT, _obj));
				btn_go.removeEventListener(MouseEvent.CLICK, createGoHandler);
				removeListener(man_con.RockManA);
				removeListener(man_con.RockManB);
				removeListener(man_con.RockManC);
				removeListener(man_con.RockManD);
				removeListener(man_con.RockManE);
				removeListener(man_con.RockManF);
				removeListener(man_con.RockManG);
				removeListener(man_con.RockManH);
			}
		}

		private function getBitmapFilter():GlowFilter {
			var color:Number = 0xCC6600;
			var alpha:Number = 0.7;
			var blurX:Number = 20;
			var blurY:Number = 20;
			var strength:Number = 2;
			var inner:Boolean = false;
			var knockout:Boolean = false;
			var quality:Number = BitmapFilterQuality.HIGH;
			return new GlowFilter(color, alpha, blurX, blurY, strength, quality, inner, knockout);
		}
/******************************************************************************/
	}
}